<template>
	<view class="content">
		<listHead :only="only" :title='written'></listHead>
		<view class="bottom bottom-ewm" v-if="nfcId" @tap="btnChang">
			{{nfcId}}
		</view>
		<view class="bottom bottom-ewm" v-else @tap="btnChang">
			请贴靠手环进行绑定
		</view>
		<view class="top">
			<view class="top-top" v-for="(item,index) in list" :key="index">
				<view class="top-left" v-if="item.title.length>2">{{item.title}}</view>
				<view class="top-left " v-else>
					<view class="top-title">
						{{item.title}}
					</view>
				</view>
				<view class="top-right">{{item.name}}</view>
			</view>
			<!-- <u-button type="primary" class="btn" text="点击扫手环二维码" color="#7ADFCA" @tap="btnChang"></u-button> -->
		</view>
		<view class="top-bottom">
			绑定结果
		</view>
		<view class="bottom" v-if="this.Really[0]" style="color: red;">
			{{this.Really[0].msg?this.Really[0].msg+'成功':''}}
		</view>
		<view class="bottom" v-else></view>
		<view class="bottom-bottom" style="padding: 0 20px;">
			<u-button type="primary" class="btn1" text="重新绑定" :color="this.Really[0]&&this.nfcId ?'#FF6E99':'#CACACA'"
				@tap="cancelswd"></u-button>
		</view>
		<hospitaldialog :show2="show" :hospitaltitle="hospitaltitle">
			<template v-slot:btn>
				<u-button type="primary" class="btn2" text="确认" color="#7ADFCA" @tap="nav"></u-button>
				<u-button type="primary" class="btn3" text="取消" color="#FF6E91" @tap="cancel"></u-button>
			</template>
		</hospitaldialog>
		<scan-code @scanCodeId="scanCodeId" value="手环绑定"></scan-code>
	</view>
</template>

<script>
	import listHead from '../../components/html/listHead.vue'
	import hospitaldialog from '../../components/html/hospitaldialog.vue'
	import testtest from "@/components/js/hexiii-nfc.js"
	import {
		ScanPatientinfo,
		BindBraceletReally,
		BindBraceletUnbind
	} from "@/api/user.js"
	import {
		mapMutations,
		mapState
	} from "vuex";
	import scanCode from "../../components/html/scan-code.vue"
	
	export default {
		components: {
			listHead,
			hospitaldialog,
			scanCode
		},
		data() {
			return {
				only: false,
				written: '手环绑定',
				show: false,
				hospitaltitle: '是否跳转到设置页面打开NFC功能',
				list: [{
						title: '床位号',
						name: ''
					},
					{
						title: '病历号',
						name: ''
					},
					{
						title: '姓名',
						name: ''
					},
					{
						title: '年龄',
						name: ''
					},
					{
						title: '性别',
						name: ''
					},
				],
				nfcId: '',
				listObj: [],
				Really: [],
				scanId: ''
			}
		},
		watch: {
			scanId: {
				handler(newval, oldval) {
					if (newval != oldval) {
						// this.nfcId = ''
						// this.Really = []
						// this.listObj = []
						// this.scanCodeFun(newval,false)
						// this.btnChang()
					}
					console.log(newval);
				}
			}
		},
		onLoad(opion) {
			let MacShow = false
			console.log(opion,'--------');
			if(opion.id ){
				
				uni.request({
					url: 'http://20.21.9.38:8022/smartcare/iconMain/queryByComId?comId=' + opion.id, //接口地址。url
					method: "get", //传输类型
					success: (res) => {
						console.log(res.data);
						if (res.data.success) {
							console.log(res.data.data.ptInfoIndex, 'res.data.data.ptInfoIndex');
							MacShow = true
							this.nfcId = opion.id
							if (res.data.data.ptInfoIndex != null) {
								 this.Really = [{msg:'捆绑'}]
								this.scanCodeFun(res.data.data.ptInfoIndex.mrn,true)
							}
						}
					}
				});
			}
			
			if (!MacShow&&opion.id) {
				this.nfcId = opion.id
			}
		},
		onShow() {
			let main = plus.android.runtimeMainActivity();
			let IntentFilter = plus.android.importClass('android.content.IntentFilter');
			let NfcAdapter = plus.android.importClass('android.nfc.NfcAdapter');
			let nfcAdapter = NfcAdapter.getDefaultAdapter(main);
			this.show = !nfcAdapter.isEnabled()
			this.testNFC()
			// var _this = this
			// uni.$on('scancodedate', function(content) {
			// 	const add = content.split('|')
			// 	_this.content = add
			// })
		},
		destroyed() {
			testtest.removeFun()
		},
		methods: {
			 cancelswd() {
				uni.showModal({
					title: '提示',
					content: '是否解绑该病人手环?',
					success:async  (res) =>{
						if (res.confirm) {
							let res =  await this.ReallyBindFun()
							console.log(res,'res');
							if(res.length>0){
								uni.showToast({
									title: '解绑成功!',
									icon:'success',
									duration: 2000
								});
								this.nfcId = ''
								this.Really = res
							
							}
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			
				
			},
			nav() {
				/*
				调出手机设置页面
				*/
				var main = plus.android.runtimeMainActivity();
				var Intent = plus.android.importClass("android.content.Intent");
				var mIntent = new Intent('android.settings.SETTINGS');
				main.startActivity(mIntent);

			},
			async testNFC() {
				//这里用异步获取读取到的NFC数据
				await testtest.listenNFCStatus().then((res) => {
				
					if (!res ) {
						this.ScanErrorAudio()
						this.nfcId = ''
						this.Really = []
					} else {
						this.nfcId = res
					}

				});
				if (!this.nfcId) {
					return
				}
				const res = await this.ReallyFun()
				console.log(res,'res');
				this.Really = res
			},
			async ReallyFun() {
				const user = uni.getStorageSync('userInfo')
				const data = {
					HosKind: user.hoscode,
					mrn: this.listObj[0].mrn,
					series: this.listObj[0].series,
					pname: this.listObj[0].pname,
					bedid: this.listObj[0].bedid,
					deptcode: user.HosKind,
					deptname: this.listObj[0].deptname,
					mac: this.nfcId
				}
				const res = await this.$http.BindBraceletReally(data)
				return res
			},
			async ReallyBindFun() {
				const user = uni.getStorageSync('userInfo')
				const data = {
					HosKind: user.hoscode,
					mrn: this.listObj[0].mrn,
					series: this.listObj[0].series,
					pname: this.listObj[0].pname,
					bedid: this.listObj[0].bedid,
					deptcode: user.HosKind,
					deptname: this.listObj[0].deptname,
					mac: this.nfcId
				}
			
				const res = await this.$http.BindBraceletUnbind(data)
				
				return res
			},
			scanCodeId(id, value) {
				console.log(id,value);
				if (value != '手环绑定') {
					return
				}
			
				this.scanId = id
				this.scanCodeFun(id)
			},
			async scanCodeFun(id,flag) {
				const user = uni.getStorageSync('userInfo')
				const data = {
					HosKind: user.hoscode,
					mrn: Number(id)
				}
				const res = await this.$http.ScanPatientinfo(data)
				console.log(res,'res');
				if (res[0].result == '') {
					uni.showLoading({
						title: '获取病人信息失败！',
						duration: 5000, //显示多少时间，默认1500毫秒
						icon: "none", //自定义显示的图标，默认成功success，错误error,加载loading，不显示图标是none
						mask: true
					});
					setTimeout(function() {
						uni.hideLoading();
					}, 2000);
					return
				}
				this.listObj = res
				const old = `${res[0].age}${res[0].ageunit == 'Y'?'岁':''}`
				this.list = [{
						title: '床位号',
						name: res[0].bedid
					},
					{
						title: '病历号',
						name: res[0].mrn
					},
					{
						title: '姓名',
						name: res[0].pname
					},
					{
						title: '年龄',
						name: old ? old : ''
					},
					{
						title: '性别',
						name: res[0].psex == 'M' ? '男' : res[0].psex == 'F' ? '女' : ''
					}
				]
				if (this.listObj.length > 0 && this.nfcId) {
					if(flag){
						return
					}
					const data = await this.ReallyFun()
					uni.showToast({
						title: '捆绑成功!',
						icon:'success',
						duration: 2000
					});
					this.Really = data
				}
			},
			cancel() {
				this.show = false
			},
			btnChang() {
				console.log(this.listObj, '222222222');
				if (this.listObj.length == 0) {
					return uni.showToast({
						title: '请先扫描腕带!',
						duration: 3000, //显示多少时间，默认1500毫秒
						icon: "error", //自定义显示的图标，默认成功success，错误error,加载loading，不显示图标是none
						mask: true
					});
				} else {
					uni.scanCode({
						success: async (res) => {
							console.log(res);
							this.msg = res.result
							const data = await this.ReallyFun()
							this.Really = data
							this.nfcId = ' '
						}
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		width: 100%;
		height: 100vh;
		overflow: hidden;
	}

	.top {
		padding: 20px 20px 0;
	}

	.top-top {
		display: flex;
		align-items: center;
		margin-bottom: 10px;

		.top-left {
			width: 22%;
			height: 36px;
			background: #B9CACB;
			border-radius: 6px 2px 2px 6px;
			font-size: 16px;
			font-family: SourceHanSansCN-Medium, SourceHanSansCN;
			font-weight: 500;
			color: #FFFFFF;
			line-height: 24px;
			display: flex;
			align-items: center;
			justify-content: center;
			border-right: 0px;
		}

		.top-title {
			width: 48px;
			text-align-last: justify;
			text-align: justify;
		}

		.top-right {
			width: 78%;
			height: 34px;
			background: #FFFFFF;
			border-radius: 0px 6px 6px 0px;
			border: 1px solid #B9CACB;
			border-left: 0px;
			display: flex;
			align-items: center;
			padding: 0 20rpx;
		}

	}

	.top-bottom {
		height: 50px;
		background: #FFFFFF;
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 21px;
		font-family: SourceHanSansCN-Normal, SourceHanSansCN;
		font-weight: 400;
		color: #333333;
		line-height: 32px;
		margin-top: 20px;
		box-shadow: 0px 2px 6px 0px rgba(0, 0, 0, 0.1);
		border-top: 1px solid rgba(0, 0, 0, 0.06);
	}

	.bottom {
		height: calc(100% - 530px);
		overflow-y: scroll;
		background: #F8F8F8;
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 24px;
	}

	.bottom-bottom {
		padding: 0 20px;
		margin-top: 20px;
	}

	.u-reset-button ::v-deep .u-button__text {
		font-size: 22px !important;
		font-family: SourceHanSansCN-Medium, SourceHanSansCN;
		font-weight: 500;
		color: #FFFFFF;
		line-height: 33px;
	}

	.btn {
		box-shadow: 0px 2px 6px 0px #6AC8B5;
		height: 56px;
	}

	.btn1 {
		box-shadow: 0px 2px 6px 0px #CACACA;
		height: 56px;
	}

	.btn2 {
		box-shadow: 0px 2px 6px 0px #6AC8B5;
	}

	.btn3 {
		box-shadow: 0px 2px 6px 0px #ff6e91;
	}

	.bottom-ewm {
		display: flex;
		justify-content: center;
		width: 100%;
		text-align: center;
		justify-content: center;
		align-items: center;
		padding-top: 10px;
		height: 54px;
		font-size: 18px;
	}
</style>
